clear all
use  "../data/zip_year_data", clear

*Define CHTV and CHTC and the IV 
gen lnchtc = log((chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash)))
gen chtc = (chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash))

gen lnchtv = log((chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval)))
gen chtv = (chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval))

by year, sort: egen chttc = sum(chtc)
gen chi_iv_ct = log(chineseper * chttc)
by year, sort: egen chttv = sum(chtv)
gen chi_iv_val = log(chineseper * chttv)

*Keep only common sample where both lnchtv and lnchtc are not missing
drop if lnchtc == .
drop if lnchtv == .

*zillow
ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtv = chi_iv_val)  if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo z1
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtc = chi_iv_ct)  if chtc != 0, absorb(timecounty) cluster(zipcode) 
eststo z2
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

*emp 
ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtv = chi_iv_val)  if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo emp1
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtc = chi_iv_ct)  if chtc != 0, absorb(timecounty) cluster(zipcode) 
eststo emp2
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)


esttab emp1 emp2 z1 z2 ///
	using "../results/tableA4.tex", replace ///
	label ///
	keep(lnchtv lnchtc) ///
	order(lnchtv lnchtc) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
	stats(c cyfe fstat N , ///
		fmt(0 0 %3.0f %11.0g ) ///
		labels("Controls" ///
			"County-year FE" ///
			"First Stage F-statistic" ///
			"Observations")) ///
	 mlabels(, none) collabels(, none) ///
	 mgroups("Total Employment" "Home Price (Zillow)", pattern(1 0 1 0))


